package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class BeiJing extends City {
	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			boolean firstrow;
			String retvalue;
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String str=(String)message.obj;
				str=str.split("处理情况")[1];
				str=str.split("</table>")[0];
				String[] rows=str.split("<tr");
				firstrow=true;
				retvalue="";
				for (int i=0;i<rows.length;i++){
					String row = rows[i];

					if (firstrow){
						firstrow=false;
						continue;
					}
					String[] cols = row.split("<td");
					String shijian = cols[2].split(">")[1].split("<")[0];
					String didian = cols[3].split(">")[1].split("<")[0];
					String code = cols[4].split(">")[2].split("<")[0];
					retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
				}
				callback.onComplete(rows.length, retvalue);
				break;
			}
		}
	};
	private Handler request2handler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				int idx=body.indexOf("#f5fdff\">");
				if (idx>0){
					body=body.substring(idx+9);
					idx=body.indexOf("<P align=\"center\">");
					if (idx>0){
						body=body.substring(idx+18);
						idx=body.indexOf("</P>");
						if (idx>0){
							callback.onComplete(-10002, "ret:"+body.substring(0, idx));
							return;
						}
					}
				}
				String str=body;
				idx=str.indexOf("违法未处理");
		        if (idx>0){
		        	idx=str.indexOf("document.form1.hpzl.value=\"");
		        	if (idx>0){
		        		String substr=str.substring(idx+27);
		        		String hpzl=substr.split("\"",2)[0];
		        		idx=str.indexOf("document.form1.yxqz.value=\"");
		        		if (idx>0){
		        			String yxqz=str.substring(idx+27).split("\"")[0];
		        			try {
		        				new HttpConnection(ctx,request1handler,"GBK").post("http://sslk.bjjtgl.gov.cn/jgjww/wzcx/wzcx_result.jsp", 
		        						String.format("hphm=%s&fdjh=%s&sf=11&yxqz=%s&hpzl=%s",
		        								URLEncoder.encode(chepai.substring(1),"UTF-8"),URLEncoder.encode(fadongjihao,"UTF-8"),
		        								yxqz,hpzl));
		        			} catch (UnsupportedEncodingException e) {
		        				e.printStackTrace();
		        			}
		        		}
		        		else{
			        		callback.onComplete(-10004,"");
		        		}
		        	}
		        	else{
		        		callback.onComplete(-10003,"");
		        	}
		        } else {
	        		callback.onComplete(0,"");
		        }				
				break;
			}
		}
	};


	private String fadongjihao="";	
	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("京");
	}

	@Override
	public String[] paramDesc() {
		String[] s={"发动机号"};
		return s;
	}

	public void requestParamsFinish(String[] values) {
		try {
			fadongjihao=values[0];
			new HttpConnection(ctx,request2handler,"GBK").post("http://sslk.bjjtgl.gov.cn/jgjww/wzcx/wzcx_preview.jsp", 
					String.format("carno=%s&fdjh=%s&sf=11",
							URLEncoder.encode(chepai.substring(1),"UTF-8"),URLEncoder.encode(values[0],"UTF-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}


}
